package com.migo.thread;

import com.migo.entity.ShOrderInfoEntity;
import com.migo.entity.ShOrderStatusLineEntity;
import com.migo.mongo.service.AfterSaleMongoService;
import com.migo.oss.OSSFactory;
import com.migo.service.ShOrderInfoService;
import com.migo.service.ShOrderStatusLineService;
import com.migo.utils.JsonUtils;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xuzr on 2021/04/13.
 */
public class AfterSaleUserReceivingThread implements Runnable {

    private ShOrderInfoService shOrderInfoService;

    private ShOrderStatusLineService shOrderStatusLineService;

    private AfterSaleMongoService afterSaleMongoService;

    private String params;
    private OSSFactory ossFactory;
    private Logger log=Logger.getLogger(AfterSaleUserReceivingThread.class);

    public AfterSaleUserReceivingThread(String params, ShOrderInfoService shOrderInfoService, ShOrderStatusLineService shOrderStatusLineService, AfterSaleMongoService afterSaleMongoService) {
        this.params = params;
        this.shOrderInfoService = shOrderInfoService;
        this.shOrderStatusLineService = shOrderStatusLineService;
        this.afterSaleMongoService = afterSaleMongoService;
    }


    @Override
    public void run() {
        try {
            Map<String, String> mapFromJson = JsonUtils.getMapFromJson(this.params);
            String orderId = mapFromJson.get("orderId");
            /*if (orderId.indexOf("_") > 0) {
                orderId = orderId.split("_")[0];
            }*/
            if(!"".equals(orderId.trim()) && orderId!=null){
                String language = mapFromJson.get("language");

                ShOrderInfoEntity shOrderInfoTmp = shOrderInfoService.queryOrderId(orderId);
                Integer previousStatus=12 ;
                if(shOrderInfoTmp!=null){
                    previousStatus= shOrderInfoTmp.getPreviousStatus();
                }

                ShOrderInfoEntity shOrderInfo = new ShOrderInfoEntity();

                shOrderInfo.setOrderId(orderId);
                // 签收时间取后台数据库的时间
                //shOrderInfo.setOrderLogisticsCode(userSignTime);
                // 修改状态到下一步【用户确认收货】
                //判断服务类型   根据服务类型去修改状态  add by hyl 2021-07-26
                Integer serviceTypeId = shOrderInfoTmp.getServiceTypeId();
                if (serviceTypeId == 1){
                    shOrderInfo.setOrderStatusId(12);
                }else {
                    shOrderInfo.setOrderStatusId(8);
                }

                shOrderInfo.setPreviousStatus(previousStatus);
                this.shOrderInfoService.updateUserReceiving(shOrderInfo);
                ShOrderInfoEntity shOrderInfoNew = this.shOrderInfoService.queryOrderId(orderId);
                //后续需要添加状态更新记录--添加操作日志
                Map<String, Object> shOrderInfoMap=new HashMap<String, Object>();
                shOrderInfoMap.put("orderStatusId",Integer.valueOf(11));
                shOrderInfoMap.put("serviceType",Integer.valueOf(1));
                shOrderInfoMap.put("language",language);
                List<ShOrderStatusLineEntity>  shOrderStatusLineList = shOrderStatusLineService.queryListByStatusId(shOrderInfoMap);
                if(shOrderStatusLineList!=null && shOrderStatusLineList.size()>0){
                    //用户已收货
                    afterSaleMongoService.saveAfterLog(shOrderInfoNew, shOrderStatusLineList.get(0).getDepict());
                }else {
                    log.info("线程="+Thread.currentThread().getId()+"：后台的流程状态sh_order_status_line多语言("+language+")未配置，请联系管理员配置！");
                }


            }else{
                log.info("线程="+Thread.currentThread().getId()+"：上送的订单ID空，跳过不进行处理。");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
